package com.tjsinfo.tjsoa.system.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.system.vo.LianDong;

public class LianDongBO implements ILianDongBO {
	private ISpringDao springDao;
	
	
	public ISpringDao getSpringDao() {
		return springDao;
	}


	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}


	public List<LianDong> selectAllLianDong() {
		// TODO Auto-generated method stub
		List<LianDong> list = new ArrayList<LianDong>();
		StringBuffer sql = new StringBuffer("SELECT * FROM dbo.zhusongliandong order by sort");
		list =  springDao.querySQL(sql.toString(), new Object[]{}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				LianDong lianDong = new LianDong();
				lianDong.setId(arg0.getInt("id"));
				lianDong.setLianDongId(arg0.getString("liandongid"));
				lianDong.setLianDongLable(arg0.getString("liandonglable"));
				lianDong.setLianDongName(arg0.getString("liandongname"));
				lianDong.setSort(arg0.getString("sort"));
				return lianDong;
			}
			
		});
		return list;
	}

	public Boolean addLianDong(LianDong lianDong){
		StringBuffer sql = new StringBuffer("INSERT INTO [dbo].[zhusongliandong]([liandonglable],[liandongname],[liandongid],[sort]) ");
		sql.append("VALUES(?,?,?,?) ");
		springDao.commonUpdate(sql.toString(), new Object[]{lianDong.getLianDongLable(),lianDong.getLianDongName(),lianDong.getLianDongId(),lianDong.getSort()});
		return true;
	}
	
	public LianDong selectLianDongById(Integer id){
		List<LianDong> list = new ArrayList<LianDong>();
		StringBuffer sql = new StringBuffer("SELECT * FROM dbo.zhusongliandong WHERE id = ?");
		list =  springDao.querySQL(sql.toString(), new Object[]{id}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				LianDong lianDong = new LianDong();
				lianDong.setId(arg0.getInt("id"));
				lianDong.setLianDongId(arg0.getString("liandongid"));
				lianDong.setLianDongLable(arg0.getString("liandonglable"));
				lianDong.setLianDongName(arg0.getString("liandongname"));
				lianDong.setSort(arg0.getString("sort"));
				return lianDong;
			}
			
		});
		return list.get(0);
	}
	
	public Boolean  updateLianDong(LianDong lianDong){
		StringBuffer sql = new StringBuffer("UPDATE [dbo].[zhusongliandong] SET [liandonglable] = ?,[liandongname] = ?,[liandongid] = ?,[sort]=? ");
		sql.append("WHERE id = ? ");
		springDao.commonUpdate(sql.toString(), new Object[]{lianDong.getLianDongLable(),lianDong.getLianDongName(),lianDong.getLianDongId(),lianDong.getSort(),lianDong.getId()});
		return true;
	}


	public void addUser(String liandongids, Integer id, String userName) {
		// TODO Auto-generated method stub
		String[] idarray = liandongids.split(",");
		for(String liandongid :idarray){
			LianDong liandong = this.selectLianDongById(Integer.valueOf(liandongid));
			liandong.setLianDongName(liandong.getLianDongName()+userName +",");
			liandong.setLianDongId(liandong.getLianDongId()+String.valueOf(id)+",");
			this.updateLianDong(liandong);
		}
	}
}
